import Request from './request'
const http = new Request()
let baseUrl = 'https://cnpe.knew-how.com' //公共baseUrl
try {
	// if (location.hostname == 'localhost') {
	// 	baseUrl = ''
	// }
} catch (e) {}
// request全局参数设置
http.setConfig(config => {
	/* 设置全局配置 */
	config.baseUrl = baseUrl
	config.header = {
		...config.header,
	}
	console.log('222')
	console.log(config)
	return config
})
http.interceptor.request(config => {
	/* 请求之前拦截器 */
	config.header['token'] = uni.getStorageSync('token') || 'd71ad8b5-4ccc-41ea-878e-bf7f950d8321';
	// config.header['token']= '3780ac5f-c4b3-467a-95b9-45c4fb53594f';
	// config.header['token'] = '367acac7-0333-4fb5-acca-39e4ce7f3302'
	// config.header['shop_id'] = 1
	config.header['shop_id'] = uni.getStorageSync('shop_id');
	return config
}, error => {
	return Promise.reject(error)
})
http.interceptor.response(async response => {
	/* 请求之后拦截器 */
	switch (response.data.code || response.data.status) {
		case 1:
			return response.data
		case 101:
			break
		case 200:
			return response.data
		case 202:
			return response.data
			break
		case 203:
			break
		case 10001:
		case 1004:
			uni.removeStorageSync('token')
			// uni.showToast({
			// 	icon: 'none',
			// 	title: '用户凭证过期请重新登录！'
			// });
			uni.switchTab({
				url: '/pages/Tabbar/Mine/Mine'
			})
			break
		case 400:
			return Promise.reject(response.data.msg)
			break
		case 401:
			uni.showModal({
				title: '提示',
				content: '请先登录然后在进行下一步操作！',
				success: function(res) {
					// uni.reLaunch({
					// 	// url: '/pages/Login/LoginHome',
					// 	url: '/pages/Tabbar/Home/Home',
					// 	fail(e) {
					// 		console.log(e)
					// 	}
					// })
				}
			})
			break;
		case 404:
			return Promise.reject(response.data.msg)
		default:
			uni.showToast({
				icon: 'none',
				title: response.data.msg
			})
			return response.data
	}
}, error => {
	console.log(error)
	if (error.data.code == 500 || error.statusCode == 500) {
		uni.showToast({
			icon: 'none',
			title: error.data.msg,
			duration: 4000,
			success: function() {
				// 延迟2秒后跳转页面
				setTimeout(function() {
					uni.reLaunch({
						// url: '/pages/Login/LoginHome',
						url: '/pages/Tabbar/Home/Home',
						fail(e) {
							console.log(e)
						}
					})
				}, 2000);
			}
		});
	}
	if (error.data.code == 501 || error.statusCode == 501) {
		uni.showToast({
			icon: 'none',
			title: error.data.msg
		})
		setTimeout(function() {
			uni.reLaunch({
				url: '/pages/error/error',
			});
		}, 2000);
		return false;
	}
	//console.log('http requset error:',JSON.stringify(JSON.parse(error)))
	//监听401状态，则是401跳到登录
	if (error.statusCode == 401 || error.data.code == 401) {
		// uni.reLaunch({
		// 	// url: '/pages/Login/LoginHome',
		// 	url: '/pages/Tabbar/Home/Home',
		// 	fail(e) {
		// 		console.log(e)
		// 	}
		// })
	}
	return Promise.reject(error)
})
export {
	http,
	baseUrl,
}